local skel = fk.CreateSkill {
  name = "lb__xiezhen",
}

Fk:loadTranslationTable {
  ["lb__xiezhen"] = "谐振",
  [":lb__xiezhen"] = "每回合每项限一次，若「<b>回响</b>」存在以下组合：" ..
      "1.花色相同且点数不同，你可将一张该花色的牌当其中一张基本牌或普通锦囊牌使用或打出。" ..
      "2.点数相同且花色不同，当你失去该点数的牌后，摸其中不同花色数张牌。",

  ["#lb__xiezhen"] = "谐振：你可以将一张与「<b>回响</b>」花色相同的牌当与之牌名相同的基本牌或普通锦囊牌使用",

  ["$lb__xiezhen1"] = "得以拜谒星神的原因？因为我是黑塔啊。",
  ["$lb__xiezhen2"] = "有人问我灵感是什么，不就是脑中一分钟几千次的波动吗？",
}

---@param player Player
---@return table
local function getHuiXiangviewlist(player)
  local cardIds = player:getPile("lb__gubo_huixiang")
  local combinations = {}

  -- 按花色分组，每个花色下存储不同点数的卡牌
  local suitGroups = {}

  for _, id in ipairs(cardIds) do
    local card = Fk:getCardById(id)
    if card then
      local suit = card:getSuitString()
      local number = card.number
      local name = card.name

      -- 初始化该花色的分组
      if not suitGroups[suit] then
        suitGroups[suit] = {}
      end

      -- 如果该点数还没有记录，则添加卡牌名
      if not suitGroups[suit][number] then
        suitGroups[suit][number] = {}
      end
      table.insert(suitGroups[suit][number], name)
    end
  end

  -- 筛选出花色相同且点数不同的组合

  for suit, numberMap in pairs(suitGroups) do
    -- 如果该花色有至少2个不同的点数，则添加到结果中
    local num = 0
    for _, _ in pairs(numberMap) do
      num = num + 1
    end

    if num > 1 then
      combinations[suit] = numberMap
    end
  end

  return combinations
end

skel:addEffect("viewas", {
  max_turn_use_time = 1,
  anim_type = "offensive",
  pattern = ".",
  prompt = "#lb__xiezhen",
  interaction = function(self, player)
    local list = getHuiXiangviewlist(player)
    local all_choices = {}
    for _, numberMap in pairs(list) do
      for _, name in pairs(numberMap) do
        table.insertTableIfNeed(all_choices, name)
      end
    end
    all_choices = table.filter(Fk:getAllCardNames("bt"), function(name)
      return table.contains(all_choices, name)
    end)
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function(self, player, to_select, selected, selected_targets)
    if #selected == 0 and self.interaction.data then
      local card = Fk:getCardById(to_select)
      local list = getHuiXiangviewlist(player)
      if list[card:getSuitString()] then
        local all_choices = {}
        for _, numberMap in pairs(list[card:getSuitString()]) do
          table.insertTableIfNeed(all_choices, numberMap)
        end
        return table.contains(all_choices, self.interaction.data)
      end
    end
    return false
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return nil end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    card:addSubcards(cards)
    return card
  end,
  enabled_at_play = function(self, player)
    return not player:isNude() and #player:getPile("lb__gubo_huixiang") > 1
  end,
  enabled_at_response = function(self, player, response)
    if not player:isNude() and #player:getPile("lb__gubo_huixiang") > 1 then
      if Fk.currentResponsePattern then
        local list = getHuiXiangviewlist(player)
        for _, numberMap in pairs(list) do
          for _, names in pairs(numberMap) do
            for _, name in pairs(names) do
              if Exppattern:Parse(Fk.currentResponsePattern):matchExp(name) then
                return true
              end
            end
          end
        end
      else
        return true
      end
    end
  end,
}, { check_effect_limit = true})

skel:addEffect(fk.AfterCardsMove, {
  max_turn_use_time = 1,
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or #player:getPile("lb__gubo_huixiang") < 2 then return false end
    local numberGroups = {}
    for _, id in ipairs(player:getPile("lb__gubo_huixiang")) do
      local card = Fk:getCardById(id)
      if card then
        local number = tostring(card.number)
        if not numberGroups[number] then
          numberGroups[number] = {}
        end
        table.insertIfNeed(numberGroups[number], card.suit)
      end
    end
    for _, move in ipairs(data) do
      if move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if numberGroups[tostring(Fk:getCardById(info.cardId).number)] then
              local num = 0
              for _, _ in pairs(numberGroups[tostring(Fk:getCardById(info.cardId).number)]) do
                num = num + 1
              end
              if num > 1 then
                event:setCostData(self, { num = num })
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(event:getCostData(self).num, skel.name)
  end,
}, { check_effect_limit = true})


return skel